<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*****************************************************************************
 * 
 * Copyright [2013] [Mervin.Wong]
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 *       
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *  
 *****************************************************************************/
 
/**
 * CanGoo Index 控制器
 *
 *	主要用于
 *
 * @package		CanGoo
 * @subpackage	Controllers
 * @category	Admin-controllers
 * @author		Mervin.Wong<mervin0502@163.com
 * @link		http://mervin.me/project/cangoo
 */ 
 class Index extends CI_Controller{
 	/**
 	 * 构造函数
 	 * @access public 
 	 * @return void
 	 */
 	public function __construct(){
 		parent::__construct();
 		$this->load->helper('url');
 		$this->load->library('session');
 		$this->load->model('index_model');
 	}
 	/**
 	 * 默认执行方法
 	 * @access public
 	 * @return void
 	 */
 	public function index(){
 		if($this->_is_login()){
 			$this->load->view('index_view');
 		}else{
 			redirect(site_url('index/login', 'refresh'));
 		}
 	}
 	
 	/**
 	 * 载入top页面
 	 * @access public
 	 * @return void
 	 */
 	public function top(){
 		if($this->_is_login()){
 			$this->load->view('top_view');
 		}else{
 			redirect(site_url('index/login', 'refresh'));
 		}
 	}
 	/**
 	 * 载入menu页面
 	 * @access public
 	 * @return void
 	 */
 	public function menu(){
 		if($this->_is_login()){
 			$this->load->view('menu_view');
 		}else{
 			redirect(site_url('index/login', 'refresh'));
 		}
 	}
 	/**
 	 * 载入main页面
 	 * @access public
 	 * @return void
 	 */
 	public function main(){
 		if($this->_is_login()){
 			$this->load->view('main_view');
 		}else{
 			redirect(site_url('index/login', 'refresh'));
 		}
 	}
 	/**
 	 * 载入footer页面
 	 * @access public
 	 * @return void
 	 */
 	public function footer(){
 		if($this->_is_login()){
 			$this->load->view('footer_view');
 		}else{
 			redirect(site_url('index/login', 'refresh'));
 		}
 	}
 	
 	/**
 	 * 登录
 	 */
 	public function login(){
 		if($this->session->userdata('status') != 'login'){
 			$this->load->helper('form');
 			//未登录
	 		if($this->input->post('is_submit') != 'ok'){
	 			//未提交表单
	 			$form = form_open(site_url('index/login'),array('name'=>'login_form', 'id'=>'login_form'));
	 			$form .= '<p>';
	 			$form .= form_label('用户名:', 'login_name');
	 			$form .= form_input(array('name'=>'login_name', 'id'=>'login_name', 'size'=>12));
	 			$form .= '</p><p>';
	 			$form .= form_label('用户密码:', 'login_password');
	 			$form .= form_password(array('name'=>'login_password', 'id'=>'login_password', 'size'=>12));
	 			$form .= '</p><p>';
	 			$form .= form_hidden('is_submit', 'ok');
	 			$form .= form_reset('login_reset', '重写');
	 			$form .= form_submit('login_submit', '提交');
	 			$form .= form_close();
	 			
	 			$data['login_form'] = $form ;
	 			$this->load->view('login_view', $data);
	 		}else{
	 			//提交表单
	 			//$this->load->library('form_validation');
	 			$name = $this->input->post('login_name');
	 			$password = md5($this->input->post('login_password'));
	 			//登录成功时，将session的status->yes
	 			$msg = $this->index_model->login($name, $password, $this->session->userdata('session_id'));
	 			if(is_array($msg)){
	 				$this->session->set_userdata(array('uid'=>$msg['uid'], 'name'=>$msg['name'], 'status'=>'login'));
	 				redirect(site_url(),'refresh');
	 			}else{
	 				$form = form_open(site_url('index/login'),array('name'=>'login_form', 'id'=>'login_form'));
	 				$form .= '<p>';
	 				$form .= form_label('用户名', 'login_name');
	 				$form .= form_input(array('name'=>'login_name', 'id'=>'login_name', 'size'=>12));
	 				$form .= '</p><p>';
	 				$form .= form_label('用户密码:', 'login_password');
	 				$form .= form_password(array('name'=>'login_password', 'id'=>'login_password', 'size'=>12));
	 				$form .= '</p><p>';
	 				$form .= $msg;
	 				$form .= '</p><p>';
	 				$form .= form_hidden('is_submit', 'ok');
	 				$form .= form_reset('login_reset', '重写');
	 				$form .= form_submit('login_submit', '提交');
	 				$form .= form_close();
	 				
	 				$data['login_form'] = $form ;
	 				$this->load->view('login_view', $data);	 				
	 			}
	 		}
 		}else{
 			//已登录，跳转
 			redirect(site_url(), 'refresh');
 		}
 		
 	}
 	
 	public function logout(){
 		$session_id = $this->session->userdata('session_id');
 		$this->session->unset_userdata(array('uid'=>0, 'name'=>'visitor', 'status'=>'logout'));
 		$this->index_model->logout($session_id);
 		redirect(site_url(), 'refresh');
 	}
 	public function welcome(){
 		$this->load->view('welcome_message');
 	}
 	
 	
 	private function _is_login(){
 		$session_id = $this->session->userdata('session_id');
 		$this->load->model('common_model');
 		return $this->common_model->is_login($session_id);
 	}
 }
/* End of file index.php */
/* Location: ./apps/admin/controllers/index.php */